package com.ihealthway.all.groupbuy.services;

import com.ihealthway.all.groupbuy.vo.Company;
import org.nutz.dao.Cnd;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.*;

/**
 * Company service
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean(fields = {"dao"})
public class CompanyService extends BaseService<Company> {

    private static final Log logger = Logs.get();

    public boolean getExist(String companyUUId) {

        boolean exist = false;

        try {
            Criteria cri = Cnd.where("c_uuid", "=", companyUUId).and("data_status","=","0");

            List<Company> companies = query(cri);

            exist = (companies!=null && !companies.isEmpty());
        } catch (Exception e) {
            logger.errorf("company UUID: %s", companyUUId);
            logger.error("Check exist company failed.", e);
        }

        return exist;
    }

    public boolean add(Company company) {

        boolean add = false;

        company.setCreateTime(new Date());
        company.setDataStatus("0");
        try {
            Company insertTemp = _insert(company);
            add = insertTemp != null;
        } catch (Exception e) {
            logger.error(company);
            logger.error("Insert new company info failed.", e);
        }

        return add;
    }

    public boolean update(Company company) {

        boolean update = false;

        company.setUpdateTime(new Date());
        try {
            int effectRows = _updateIgnoreNull(company);
            update = effectRows != 0;
        } catch (Exception e) {
            logger.error(company);
            logger.error("Update company info failed.", e);
        }

        return update;
    }

    public List<Company> getCompany() {

        List<Company> companies = new ArrayList<>();

        Cnd cnd = Cnd.where("data_status", "=","0");
        cnd.orderBy("c_uuid", "asc");
        try {
            companies.addAll(query(cnd));
        }catch (Exception e) {
            logger.error("Get company info failed!!!", e);
        }

        return companies;
    }
}
